using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace MobileEditor
{
	/// <summary>
	/// A custom file dialog.
	/// </summary>
	/// <remarks>
	/// <para>Authors</para>
	/// <ul>
	/// <li>KEB - Karl E Bradshaw</li>
	/// </ul>
	/// <para>Version History</para>
	/// <table border="1">
	/// <tr><td>Date</td><td>Author</td><td>Change No</td><td>Description</td></tr>
	/// <tr><td>2007-03-05</td><td>KEB</td><td></td><td>Initial version.</td></tr>
	/// </table>
	/// </remarks>
	public partial class FileExplorerForm : Form
	{
		/// <summary>
		/// Initializes an instance of the FileExplorerForm class.
		/// </summary>
		public FileExplorerForm()
		{
			InitializeComponent();
		}


		public bool EnablePathChange
		{
			get { return btnBrowsePath.Enabled; }
			set { btnBrowsePath.Enabled = value; }
		}

		public bool FileMustExist
		{
			get { return !txtFilename.Enabled; }
			set { txtFilename.Enabled = !value; }
		}

		/// <summary>
		/// True to allow multiple selection using checkboxes.
		/// </summary>
		public bool MultiSelect
		{
			get { return lvFiles.CheckBoxes; }
			set { lvFiles.CheckBoxes = value; }
		}

		public string SelectedFile
		{
			get { return Path.Combine(txtPath.Text, txtFilename.Text); }
			set
			{
				txtPath.Text = Path.GetDirectoryName(value);
				txtFilename.Text = Path.GetFileName(value);
			}
		}

		/// <summary>
		/// The selected files.
		/// </summary>
		public IList<string> SelectedFiles
		{
			get
			{
				IList<string> files = new List<string>();
				foreach (ListViewItem item in lvFiles.Items)
				{
					if (item.Selected || item.Checked) files.Add((string)item.Tag);
				}
				return files;
			}
		}

		/// <summary>
		/// The selected filename mask.
		/// </summary>
		public FilenameMask SelectedMask
		{
			get { return (FilenameMask)cboMask.SelectedItem; }
			set
			{
				AddMask(value);
				cboMask.SelectedItem = value;
			}
		}

		/// <summary>
		/// The selected path.
		/// </summary>
		public string SelectedPath
		{
			get { return txtPath.Text; }
			set { txtPath.Text = value; }
		}


		/// <summary>
		/// Adds a filename mask to the form.
		/// </summary>
		/// <param name="mask">The mask to add.</param>
		public void AddMask(FilenameMask mask)
		{
			if (!cboMask.Items.Contains(mask)) cboMask.Items.Add(mask);
		}

		/// <summary>
		/// Adds multiple filename masks to the form.
		/// </summary>
		/// <param name="masks">The masks to add.</param>
		public void AddMasks(ICollection<FilenameMask> masks)
		{
			foreach (FilenameMask mask in masks) AddMask(mask);
		}

		/// <summary>
		/// Refreshes the list of files.
		/// </summary>
		private void RefreshFiles()
		{
			List<string> files = new List<string>();
			if (Directory.Exists(txtPath.Text))
			{
				try
				{
					files.AddRange(Directory.GetFiles(txtPath.Text, (SelectedMask != null ? SelectedMask.Mask : "*.*")));
				}
				catch
				{
				}
			}
			files.Sort();

			lvFiles.BeginUpdate();
			try
			{
				lvFiles.Items.Clear();
				foreach (string file in files) lvFiles.Items.Add(new ListViewItem(Path.GetFileName(file)));
			}
			finally
			{
				lvFiles.EndUpdate();
			}
		}


		/// <summary>
		/// Handles the SelectedIndexChanged event of cboMask.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void cboMask_SelectedIndexChanged(object sender, EventArgs e)
		{
			RefreshFiles();
		}

		/// <summary>
		/// Handles the Load event of FileExplorerForm.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void FileExplorerForm_Load(object sender, EventArgs e)
		{

		}

		/// <summary>
		/// Handles the TextChanged event of txtPath.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void txtPath_TextChanged(object sender, EventArgs e)
		{
			RefreshFiles();
		}

		private void lvFiles_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (lvFiles.SelectedIndices.Count > 0)
			{
				txtFilename.Text = lvFiles.Items[lvFiles.SelectedIndices[0]].Text;
			}
		}

		private void btnBrowsePath_Click(object sender, EventArgs e)
		{
			PathExplorerForm frm = new PathExplorerForm();
			if (frm.ShowDialog() == DialogResult.OK)
			{
				SelectedPath = frm.SelectedPath;
				RefreshFiles();
			}
		}
	}
}